$(function(){
	
	if(user == null || user == ''){
   		$("#loginOrRegist").show();
   	}else {
   		$("#top-index-usersName").text(user.usersName);
   		$("#usersName").show();
   	}
	
	if(user.usersType == "2"){
		$("#shoppings").show();
		$("#showOrderHair").show();
	}
	
	$("#users_name").text(user.usersName);
	var usersType = user.usersType;
	var usersImage = user.usersImage;
	if(usersType == "1"){
		$("#business").show();
	}else if(usersType == "2"){
		$("#real_name").show();
	}
	if(usersImage != "" && usersImage != null){
		$("#user_images").attr("src","../"+usersImage);
	}else {
		$("#user_images").attr("src","../image/img_logged_in.png");
	}
	
})

function show(str){
	var obj = $(".show");
	$(obj).attr("class","fr u-4-5");
	$(obj).hide();
	$("#"+str).show();
	$("#"+str).attr("class","fr u-4-5 show");
}
layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	  
	  var $ = layui.$;
	  //登录
	  $("#top-index-loginUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'login.html'
			});
	  });
	  //注册
	  $("#top-index-registUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'regist.html'
			});
	  });
	  //实名认证
	  $("#real_name").click(function(){
		  layer.open({
			  title: '实名认证'
			  ,type: 2
			  ,area: ['50%','74%']
			  ,content: 'realName.html'
			});
	  });
	  //个人信息
	  $("#personal").click(function(){
		  layer.open({
			  title: '个人信息'
			  ,type: 2
			  ,area: ['40%','90%']
		  	  ,content: 'userMessage.html'
		  });
	  });
	  
	  //成为商家
	  $("#business").click(function(){
		  layer.open({
			  title: '成为商家'
			  ,type: 2
			  ,area: ['50%','74%']
			  ,content: 'cwbusiness.html'
		  })
	  });
	  
	  if(user != null && user != ""){
			//申请成为商家的审核提示
			  if(user.isva == "2"){
				  layer.open({
					  title: '提示',
					  content: '您提交的成为商家的审核,管理员审核通过,恭喜您成为商家,现在您可以去个人中心添加您的商品',
					  btn: ['确定'],
					  yes: function(index,layero){
						  var url = "../user/saveOrUpdate.action";
						  var data = {"usersId":user.usersId,"isva":"3"};
						  $.ajax({
							  url:url,
							  data:data,
							  success:function(msg){
								  parent.location.reload();
							  }
						  });
					  }
				  });
			  }else if(user.isva == "0"){
				  layer.open({
					  title: '提示',
					  content: '您提交的成为商家的审核,管理员审核不通过',
					  btn: ['确定'],
					  yes: function(index,layero){
						  var url = "../user/saveOrUpdate.action";
						  var data = {"usersId":user.usersId,"isva":"3"};
						  $.ajax({
							  url:url,
							  data:data,
							  success:function(msg){
								  parent.location.reload();
							  }
						  });
					  }
				  });
			  }
		}
	  
	  //保存收货地址
	  $("#button-ok").click(function(){
		  var addressId = $("#addressId").val();
		  var addressPeople = $("#addressPeople").val();
		  var addressPhone = $("#addressPhone").val();
		  var dizhi = $("#city").val();
		  var detailAddress = $("#detailAddress").val();
		  var arr = dizhi.split("/");
		  var province = "";
		  var city = "";
		  var county = "";
		  
		  if((addressPeople != null && addressPeople != "") && (addressPhone != null && addressPhone != "")
				  && (dizhi != null && dizhi != "") && (detailAddress != null && detailAddress != "")){
			  
			  if(arr.length == 2){
					 province = arr[0];
					  city = arr[1];
				  }else if(arr.length == 3){
					  province = arr[0];
					  city = arr[1];
					  county = arr[2];
				  }
				  var data = {"userId":user.usersId,"addressId":addressId,"addressPeople":addressPeople,"addressPhone":addressPhone,"province":province,"city":city,"county":county,"detailAddress":detailAddress};
				  var url = "../address/saveOrUpdate.action";
				  $.ajax({
					  url:url,
					  data:data,
					  success:function(msg){
						  if(msg.state == "1"){
							  layer.msg(msg.msg,{icon:1,time:1000});
							  findAddress();
							  reset();
						  }else {
							  layer.msg(msg.msg,{icon:2,time:1000});
						  }
					  }
				  });
			  
		  }else {
			  layer.msg("请把相关信息填写完善!",{icon:5,time:1000,anim:6});
		  }
		  
		  
	  });
	  
	  //添加商品信息
	  $("#addProduct").click(function(){
		  layer.open({
			  title: '商品'
			  ,type: 2
			  ,area: ['70%','76%']
			  ,content: 'productAddOrUpdate.html?action=add'
		  })
	  });
	  
	  //查询订单
	  $("#showOrder").click(function(){
		  var data = {"ordersState":"0"};
		  findOrder(data);
	  });
	  
	  //查询待我发货订单
	  $("#showOrderHair").click(function(){
		  show_order_hair();
	  });
	  
	//限制手机号码数字输入
	  $("#addressPhone").keyup(function(event){
			var obj = event.target;
			var phone = $(obj).val();
			//限制只能输入数字的正则表达式
			var reg = new RegExp("^[0-9]*$");
			if(!reg.test(phone)){
				$(obj).val("");
				return false;
				//限制长度
			}else if(phone.length > 11){
				$(obj).val(phone.substring(0,11));
			}
		})
	//手机号码正则表达式
	  $("#addressPhone").blur(function(){
		var obj = event.target;
		var phone = $(obj).val();
		//验证输入有效的手机号码
		if(!(/^1[34578]\d{9}$/.test(phone))){
	    	layer.msg('请输入有效的手机号码!', {icon:5,time:1000,anim:6});
	    	$(obj).val("");
	        return false; 
	    }
	  });
	  
	});
//退出登录
function exit(str){
	var url = "../user/exit.action";
	var data = {"action":str};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			location.href = "index.html";
		}
	});
}

//查询收货地址
function findAddress(){
	var url = "../address/findByUserIdAllAddress.action";
	var data = null;
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#list-address").html("");
			if(msg != null || msg != ""){
				for(var i=0;i<msg.length;i++){
					$("#list-address").append('<div class="list-group-item" id="'+msg[i].addressId+'"><table border="0" cellpadding="0" cellspacing="0">'
											+'<tbody><tr><td class="col-name">'+msg[i].addressPeople+'</td><td class="col-address">'+msg[i].province+'&nbsp;&nbsp;'+msg[i].city+'&nbsp;&nbsp;'+msg[i].county+'&nbsp;&nbsp;'+msg[i].detailAddress+'</td>'
											+'<td class="col-tel"><p>'+msg[i].addressPhone+'</p></td><td class="col-operate"><p class="p-edit">'
											+'<a href="javascript:;" onclick="edit_address(\''+msg[i].addressId+'\');" title="编辑"><span>编辑</span></a>'
											+'</p><p class="p-del"><a href="javascript:;" onclick="del_address(\''+msg[i].addressId+'\');" title="删除"><span>删除</span></a>'
											+'</p></td></tr></tbody></table></div> ');
				}
			}
			
		}
	});
}
//清空收货地址表单数据
function reset(){
	$(".input-address").val("");
}
//删除收货地址
function del_address(addressId){
	var url = "../address/deletes.action";
	var data = {"addressId":addressId};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			if(msg.state == "1"){
				$("#"+addressId).remove();
			}
		}
	});
}
//修改收货地址
function edit_address(addressId){
	var url = "../address/findById.action";
	var data = {"addressId":addressId};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#addressId").val(msg.addressId);
			$("#addressPeople").val(msg.addressPeople);
			$("#addressPhone").val(msg.addressPhone);
			if(msg.county != null && msg.county != ""){
				$("#city").val(msg.province+"/"+msg.city+"/"+msg.county);
			}else {
				$("#city").val(msg.province+"/"+msg.city);
			}
			$("#detailAddress").val(msg.detailAddress);
		}
	});
}

//查询商品信息
function findProduct(){
	if(user.usersId == null || user.usersId == ""){
		$("#product-group-empty").attr("class","list-group-empty");
	}else {
		var url = "../product/findAll.action";
		var data = {"page":"1","limit":"100","userId":user.usersId};
		$.ajax({
			url:url,
			data:data,
			success:function(msg){
				$("#pro-list-div").html("");
				var list = msg.data;
				if(list == null || list == ""){
					$("#product-group-empty").attr("class","list-group-empty");
				}else {
					$("#product-group-item").attr("class","list-group-item");
					for(var i=0;i<list.length;i++){
						var isHide = "display:none";
						var isHides = "display:none";
						if(list[i].isva == '1' || list[i].isva == '0'){
							isHide = "display:block";
						}else if(list[i].isva == "2"){
							isHides = "display:block";
						}
						$("#pro-list-div").append('<table border="0" cellpadding="0" cellspacing="0" style="border-bottom:1px solid #red"><tbody><tr><td class="col-pro-img"><p class="p-img"><a title="'+list[i].proName+'"'
										+'href="javascript:;" target="_blank"> <img alt="'+list[i].proName+'" src="../'+list[i].imgPath+'" />'
										+'</a></p></td><td class="col-pro-info"><p class="p-name"><a title="'+list[i].proName+'" target="_blank"'
										+'href="javascript:;">'+list[i].proName+'</a></p></td><td class="col-price"><em>&yen;</em> <span>'+list[i].proPrice+'</span>'
										+'</td><td rowspan="2" class="col-operate"><p class="p-button"><a href="javascript:upperOrLowerShelf(\''+list[i].proId+'\',\'upperShelf\');" target="_blank" style="'+isHide+'"> <span>上架 </span>'
										+'</a><a href="javascript:upperOrLowerShelf(\''+list[i].proId+'\',\'lowerShelf\');" target="_blank" style="'+isHides+'"> <span>下架 </span></a></p><p class="p-link">'
										+'<a href="javascript:showImage(\''+list[i].proId+'\');">商品图片</a></p><p class="p-link"><a href="javascript:update_product(\''+list[i].proId+'\');">修改</a>'
										+'</p><p class="p-link"><a href="javascript:delete_product(\''+list[i].proId+'\');">删除</a>'
										+'</p></td></tr></tbody></table>');
					}
				}
			}
		});
	}
}

//显示图片
function showImage(proId){
	layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
		var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,upload = layui.upload //上传
		  ,element = layui.element; //元素操作
		
		layer.open({
			  title: '商品图片'
			  ,type: 2
			  ,area: ['50%','82%']
			  ,content: 'productImage.html?proId='+proId
		  })
		
	})
}
//上架或下架
function upperOrLowerShelf(proId,str){
	layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
		var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,upload = layui.upload //上传
		  ,element = layui.element; //元素操作
		
		var isva = "";
		if(str == 'upperShelf'){
			isva = "2";
		}else if(str == 'lowerShelf'){
			isva = "0";
		}
		var data = {"isva":isva,"proId":proId};
		var url = "../product/saveOrUpdate.action";
		$.ajax({
			url:url,
			data:data,
			success:function(msg){
				if(msg.state == "1"){
					layer.msg(msg.msg,{icon:1,time:1000},function(){
						findProduct();
					});
				}else {
					layer.msg(msg.msg,{icon:2,time:1000},function(){
						findProduct();
					});
				}
				
			}
		});
		
	})
}
//修改商品信息
function update_product(proId){
	layer.open({
		  title: '商品'
		  ,type: 2
		  ,area: ['70%','76%']
		  ,content: 'productAddOrUpdate.html?proId='+proId+'&action=update'
	  })
}
//删除商品信息
function delete_product(proId){
	layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
		var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,upload = layui.upload //上传
		  ,element = layui.element; //元素操作
		
		layer.confirm('您确认删除该条商品信息吗?', {title:'提示'}, function(index){
			var url = "../product/deletes.action";
			var data = {"proId":proId};
			$.ajax({
				url:url,
				data:data,
				success:function(msg){
					if(msg.state == "1"){
						layer.msg(msg.msg,{icon:1,time:1000},function(){
							findProduct();
						});
					}else {
						layer.msg(msg.msg,{icon:2,time:1000},function(){
							findProduct();
						});
					}
				}
			});
			  layer.close(index);
		});
		
	})
}

function check(obj,str){
	var data = "";
	if(str == "all"){
		data = {"ordersState":"0"};
	}else if(str == "unpaid"){
		data = {"ordersState":"1"};
	}else if(str == "nocomment"){
		data = {"ordersState":"2"};
	}else if(str == "send"){
		data = {"ordersState":"3"};
	}else if(str == "finished"){
		data = {"ordersState":"4"};
	}
	$($("#myOrder-cates li.current")).attr('class',"");
	$($(obj).attr("class","current"));
	findOrder(data);
}

//查询我的订单
function findOrder(data){
	var url = "../order/findAll.action";
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			var list = msg.data;
			$("#order-lists").html("");
			if(list != null && list != ""){
				for(var i=0;i<list.length;i++){
					var order_dfk = "display:none";
					var order_dfh = "display:none";
					var order_dsh = "display:none";
					var order_ywc = "display:none";
					
					if(list[i].ordersState == '1'){
						order_dfk = "display:block";
					}else if(list[i].ordersState == '2'){
						order_dfh = "display:block";
					}else if(list[i].ordersState == '3'){
						order_dsh = "display:block";
					}else if(list[i].ordersState == '4'){
						order_ywc = "display:block";
					}
					
					$("#order-lists").append('<div class="list-group-item" id="order_'+list[i].ordersId+'"><div class="o-info"><div class="col-info">'
								+'<input type="checkbox" class="checkbox" name="payCheck" value="11213397155" paymentstatus="2" clearorder="false" />'
								+'<span class="o-date">'+list[i].ordersDate+'</span> <span class="o-no">订单号：<a href="/member/order-11213397155" title="11213397155">'+list[i].ordersCode+'</a></span>'
								+'</div><div class="col-state"></div></div><div class="o-pro"><table border="0" cellpadding="0" cellspacing="0">'
								+'<tbody><tr><td class="col-pro-img"><p class="p-img">'
								+'<a title="'+list[i].proName+'" href="javascript:;"'
								+' target="_blank"> <img alt="'+list[i].proName+'"'
								+' src="../'+list[i].imgPath+'"/></a>'
								+'</p></td><td class="col-pro-info"><p class="p-name"><a title="'+list[i].proName+'"'
								+' target="_blank" href="javascript:;">'+list[i].proName+''
								+'</a></p></td><td class="col-price"><em>&yen;</em> <span>'+list[i].proPrice+'</span>'
								+'</td><td class="col-quty">'+list[i].ordersNumber+'</td><td rowspan="2" class="col-pay"><p><em>&yen;</em> <span>'+list[i].ordersSummoney+'</span></p></td>'
								+'<td rowspan="2" class="col-operate"><p class="p-button" style="'+order_dfk+'"><a class="" href="javascript:orders_cz(\'ljzf_order\',\''+list[i].ordersCode+'\',\''+list[i].ordersId+'\');" target="_blank"> <span>'
								+'立即支付 </span></a></p><p class="p-link" style="'+order_dfk+'">'
								+'<a href="javascript:orders_cz(\'qx_order\',\''+list[i].ordersCode+'\',\''+list[i].ordersId+'\');" data-ordercode="11213397155">取消订单</a>'
								+'</p><p class="p-link" style="'+order_dfh+'"><a href="javascript:;">待发货</a></p><p class="p-link" style="'+order_dsh+'">'
								+'<a href="javascript:orders_cz(\'qrsh_order\',\''+list[i].ordersCode+'\',\''+list[i].ordersId+'\');">确认收货</a></p><p class="p-link" style="'+order_ywc+'">'
								+'<a href="javascript:orders_wypj(\''+list[i].proId+'\');">我要评价</a></p><p class="p-link" style="'+order_ywc+'">'
								+'<a href="javascript:;">已完成</a></p></td></tr></tbody></table></div></div>');
				}
			}else {
				$("#order-lists").html('<div class="list-group-empty">您暂时没有相关记录。</div>');
			}
		}
	});
}
//订单操作
function orders_cz(str,ordersCode,ordersId){
	layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
		var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,upload = layui.upload //上传
		  ,element = layui.element; //元素操作
		
		if(str == "ljzf_order"){
			var data = {"ordersState":"2","ordersId":ordersId};
			var url = "../order/update.action";
			$.ajax({
				url:url,
				data:data,
				success:function(msg){
					if(msg.state == "1"){
						layer.msg("支付成功!",{icon:1,time:1000});
						$("#unpaid").click();
					}else {
						layer.msg("支付失败!",{icon:2,time:1000});
					}
				}
			});
		}else if(str == "qx_order"){
			layer.confirm('您确认取消该订单吗?', {title:'提示'}, function(index){
					var data = {"ordersId":ordersId};
					var url = "../order/deletes.action";
					$.ajax({
						url:url,
						data:data,
						success:function(msg){
							if(msg.state == "1"){
								layer.msg("取消成功!",{icon:1,time:1000});
								$("#unpaid").click();
							}else {
								layer.msg("取消失败!",{icon:2,time:1000});
							}
						}
					});
				  layer.close(index);
			});
		}else if(str == "qrsh_order"){
			var data = {"ordersState":"4","ordersId":ordersId};
			var url = "../order/update.action";
			$.ajax({
				url:url,
				data:data,
				success:function(msg){
					if(msg.state == "1"){
						layer.msg("操作成功!",{icon:1,time:1000});
						$("#send").click();
					}else {
						layer.msg("操作失败!",{icon:2,time:1000});
					}
				}
			});
		}else if(str == "deliverGoods"){
			var data = {"ordersState":"3","ordersId":ordersId};
			var url = "../order/update.action";
			$.ajax({
				url:url,
				data:data,
				success:function(msg){
					if(msg.state == "1"){
						layer.msg("发货成功!",{icon:1,time:1000});
						show_order_hair();
					}else {
						layer.msg("发货失败!",{icon:2,time:1000});
					}
				}
			});
		}
	})
}
function orders_wypj(proId){
	layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
		var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,upload = layui.upload //上传
		  ,element = layui.element; //元素操作
		
		layer.open({
			  title: '评价'
			  ,type: 2
			  ,area: ['40%','56%']
			  ,content: 'comment.html?proId='+proId
		  })
	})
}
//查询待我发货订单
function show_order_hair(){
	var data = null;
	var url = "../order/findHair.action";
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			$("#order-hair-list").html("");
			if(msg != null && msg != ""){
				for(var i=0;i<msg.length;i++){
					var order_ljfh = "display:none";
					var order_ddsh = "display:none";
					var order_ywc = "display:none";
					
					if(msg[i].ordersState == '2'){
						order_ljfh = "display:block";
					}else if(msg[i].ordersState == '3'){
						order_ddsh = "display:block";
					}else if(msg[i].ordersState == '4'){
						order_ywc = "display:block";
					}
					$("#order-hair-list").append('<div class="list-group-item" id="order_'+msg[i].ordersId+'"><div class="o-info"><div class="col-info">'
							+'<input type="checkbox" class="checkbox" name="payCheck" value="11213397155" paymentstatus="2" clearorder="false" />'
							+'<span class="o-date">'+msg[i].ordersDate+'</span> <span class="o-no">订单号：<a href="/member/order-11213397155" title="11213397155">'+msg[i].ordersCode+'</a></span>'
							+'</div><div class="col-state"></div></div><div class="o-pro"><table border="0" cellpadding="0" cellspacing="0">'
							+'<tbody><tr><td class="col-pro-img"><p class="p-img">'
							+'<a title="'+msg[i].proName+'" href="javascript:;"'
							+' target="_blank"> <img alt="'+msg[i].proName+'"'
							+' src="../'+msg[i].imgPath+'"/></a>'
							+'</p></td><td class="col-pro-info"><p class="p-name"><a title="'+msg[i].proName+'"'
							+' target="_blank" href="javascript:;">'+msg[i].proName+''
							+'</a></p></td><td class="col-price"><em>&yen;</em> <span>'+msg[i].proPrice+'</span>'
							+'</td><td class="col-quty">'+msg[i].ordersNumber+'</td><td rowspan="2" class="col-pay"><p><em>&yen;</em> <span>'+msg[i].ordersSummoney+'</span></p></td>'
							+'<td rowspan="2" class="col-operate"><p class="p-link" style="'+order_ljfh+'">'
							+'<a href="javascript:orders_cz(\'deliverGoods\',\''+msg[i].ordersCode+'\',\''+msg[i].ordersId+'\');">立即发货</a></p><p class="p-link" style="'+order_ddsh+'">'
							+'<a href="javascript:;">等待收货</a></p><p class="p-link" style="'+order_ywc+'">'
							+'<a href="javascript:;">已完成</a></p></td></tr></tbody></table></div></div>');
				}
			}else {
				$("#order-hair-list").html('<div class="list-group-empty">您暂时没有相关记录。</div>');
			}
		}
	});
}